﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Resources;

namespace ServicioWebRest.Areas.Api.Models
{
    public class UsuarioManager
    {
        ResourceManager rm = new ResourceManager(typeof(RecursosWeb));
        public List<Dictionary<string, object>> ObtenerUsuario(string usu, string cla)
        {
            // Create the command and the connection.
            string sql = "[sp_valida_autenticacion]'"+usu+"','"+cla+"'";
            SqlConnection con = new SqlConnection(rm.GetString("connectionString"));
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataTable ds = new DataTable();
            // Fill the DataSet.
            da.Fill(ds);
            DataTableaLista(ds);
            return DataTableaLista(ds);

        }
        public List<Dictionary<string, object>> ObtenerInfoUsuario(string usu)
        {
            // Create the command and the connection.
            string sql = "[sp_extrae_info_usuario]'" + usu + "'";
            SqlConnection con = new SqlConnection(rm.GetString("connectionString"));
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataTable ds = new DataTable();
            // Fill the DataSet.
            da.Fill(ds);
            DataTableaLista(ds);
            return DataTableaLista(ds);

        }
        /*****************************TOOL********************/
        public List<Dictionary<string, object>> DataTableaLista(DataTable dt)
        {
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new

            System.Web.Script.Serialization.JavaScriptSerializer();
            List<Dictionary<string, object>> rows =
              new List<Dictionary<string, object>>();
            Dictionary<string, object> row = null;

            foreach (DataRow dr in dt.Rows)
            {
                row = new Dictionary<string, object>();
                foreach (DataColumn col in dt.Columns)
                {
                    row.Add(col.ColumnName.Trim(), dr[col]);
                }
                rows.Add(row);
            }
            return rows;
        }
        
    }
}